package cn.iocoder.yudao.module.shop.controller.admin.storerecord;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.shop.controller.admin.storerecord.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.storerecord.ShopStoreRecordDO;
import cn.iocoder.yudao.module.shop.convert.storerecord.ShopStoreRecordConvert;
import cn.iocoder.yudao.module.shop.service.storerecord.ShopStoreRecordService;

@Tag(name = "管理后台 - 库存消耗记录")
@RestController
@RequestMapping("/shop/store-record")
@Validated
public class ShopStoreRecordController {

    @Resource
    private ShopStoreRecordService storeRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建库存消耗记录")
    @PreAuthorize("@ss.hasPermission('shop:store-record:create')")
    public CommonResult<Long> createStoreRecord(@Valid @RequestBody ShopStoreRecordCreateReqVO createReqVO) {
        return success(storeRecordService.createStoreRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新库存消耗记录")
    @PreAuthorize("@ss.hasPermission('shop:store-record:update')")
    public CommonResult<Boolean> updateStoreRecord(@Valid @RequestBody ShopStoreRecordUpdateReqVO updateReqVO) {
        storeRecordService.updateStoreRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除库存消耗记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('shop:store-record:delete')")
    public CommonResult<Boolean> deleteStoreRecord(@RequestParam("id") Long id) {
        storeRecordService.deleteStoreRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得库存消耗记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:store-record:query')")
    public CommonResult<ShopStoreRecordRespVO> getStoreRecord(@RequestParam("id") Long id) {
        ShopStoreRecordDO storeRecord = storeRecordService.getStoreRecord(id);
        return success(ShopStoreRecordConvert.INSTANCE.convert(storeRecord));
    }

    @GetMapping("/list")
    @Operation(summary = "获得库存消耗记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('shop:store-record:query')")
    public CommonResult<List<ShopStoreRecordRespVO>> getStoreRecordList(@RequestParam("ids") Collection<Long> ids) {
        List<ShopStoreRecordDO> list = storeRecordService.getStoreRecordList(ids);
        return success(ShopStoreRecordConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得库存消耗记录分页")
    @PreAuthorize("@ss.hasPermission('shop:store-record:query')")
    public CommonResult<PageResult<ShopStoreRecordRespVO>> getStoreRecordPage(@Valid ShopStoreRecordPageReqVO pageVO) {
        PageResult<ShopStoreRecordDO> pageResult = storeRecordService.getStoreRecordPage(pageVO);
        return success(ShopStoreRecordConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出库存消耗记录 Excel")
    @PreAuthorize("@ss.hasPermission('shop:store-record:export')")
    @OperateLog(type = EXPORT)
    public void exportStoreRecordExcel(@Valid ShopStoreRecordExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ShopStoreRecordDO> list = storeRecordService.getStoreRecordList(exportReqVO);
        // 导出 Excel
        List<ShopStoreRecordExcelVO> datas = ShopStoreRecordConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "库存消耗记录.xls", "数据", ShopStoreRecordExcelVO.class, datas);
    }

    @PostMapping("/month")
    @Operation(summary = "某个月的库存记录")
    public CommonResult<List<ShopStoreRecordRespVO>> getStoreListByTime(@Valid @RequestBody ShopStoreRecordListReqVO reqVO){
        DateTime time = new DateTime(reqVO.getDateTime());
        DateTime startTime = DateUtil.beginOfMonth(time);
        DateTime endTime = DateUtil.endOfMonth(time);

        List<ShopStoreRecordDO> list = storeRecordService.getStroeRecordListByTime(startTime.toLocalDateTime(), endTime.toLocalDateTime(), reqVO.getTenantId(), reqVO.getType());
        return success(ShopStoreRecordConvert.INSTANCE.convertList(list));
    }

}
